<!--
 * @FilePath: /web-vue/src/views/SysLog/addForm.vue
 * @Description: 【系统日志】新增表单
-->
<template>
  <div>
    <!-- 弹出层-表单 -->
    <el-dialog
      class="add-dialog"
      title="新增【系统日志】"
      :visible.sync="visible"
      :confirmLoading="confirmLoading"
      top="7vh"
      :close-on-click-modal="false"
      :close-on-press-escape="true"
      :fullscreen="false"
    >
      <el-form
        :model="addForm"
        :rules="rules"
        ref="addForm"
        status-icon
        label-width="150px"
        style="width: 100%"
        class="layer-form-style"
      >
        <el-form-item label="ID：" prop="id" size="mini">
          <el-input v-model="addForm.id" placeholder="请输入ID" clearable style="width:99%"></el-input>
        </el-form-item>
        <el-form-item label="操作人ID：" prop="operatorId" size="mini">
          <el-input v-model="addForm.operatorId" placeholder="请输入操作人ID" clearable style="width:99%"></el-input>
        </el-form-item>
        <el-form-item label="操作内容：" prop="operation" size="mini">
          <el-input v-model="addForm.operation" placeholder="请输入操作内容" clearable style="width:99%"></el-input>
        </el-form-item>
        <el-form-item label="请求方式：" prop="requestMethod" size="mini">
          <el-input v-model="addForm.requestMethod" placeholder="请输入请求方式" clearable style="width:99%"></el-input>
        </el-form-item>
        <el-form-item label="资源路径：" prop="resourcePath" size="mini">
          <el-input v-model="addForm.resourcePath" placeholder="请输入资源路径" clearable style="width:99%"></el-input>
        </el-form-item>
        <el-form-item label="状态：" prop="status" size="mini">
          <el-select
            v-model="addForm.status"
            filterable
            clearable
            placeholder="请选择状态"
            style="width:99%"
          >
            <el-option
              v-for="item in $store.state.DICT.apiStatusSelect"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="耗时：" prop="consumeTime" size="mini">
          <el-input v-model="addForm.consumeTime" placeholder="请输入耗时" clearable style="width:99%"></el-input>
        </el-form-item>
        <el-form-item label="操作时间：" prop="operationTime" size="mini">
          <el-date-picker
            v-model="addForm.operationTime"
            type="datetime"
            placeholder="请选择操作时间"
            align="right"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="DateTime_Picker_Option">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户端IP：" prop="clientIp" size="mini">
          <el-input v-model="addForm.clientIp" placeholder="请输入客户端IP" clearable style="width:99%"></el-input>
        </el-form-item>
        <el-form-item label="详细信息：" prop="detailMsg" size="mini">
          <el-input
            type="textarea"
            v-model="addForm.detailMsg"
            :autosize="{ minRows: 3, maxRows: 9 }"
            placeholder="请输入详细信息"
            clearable
            style="width:99%"
          ></el-input>
        </el-form-item>
        <el-form-item label="异常信息：" prop="exceptionMsg" size="mini">
          <el-input
            type="textarea"
            v-model="addForm.exceptionMsg"
            :autosize="{ minRows: 3, maxRows: 9 }"
            placeholder="请输入异常信息"
            clearable
            style="width:99%"
          ></el-input>
        </el-form-item>
        <el-form-item label="操作人：" prop="operator" size="mini">
          <el-input v-model="addForm.operator" placeholder="请输入操作人" clearable style="width:99%"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          icon="el-icon-finished"
          @click="handleSubmit()"
          >保存
        </el-button>
        <el-button icon="el-icon-refresh-left" @click="handleCancel()"
          >取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const DateTime_Picker_Option = {
  shortcuts: [{
    text: '今天',
    onClick(picker) {
      picker.$emit('pick', new Date());
    }
  }, {
    text: '昨天',
    onClick(picker) {
      const date = new Date();
      date.setTime(date.getTime() - 3600 * 1000 * 24);
      picker.$emit('pick', date);
    }
  }, {
    text: '一周前',
    onClick(picker) {
      const date = new Date();
      date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
      picker.$emit('pick', date);
    }
  }]
}

export default {
  data() {
    return {
      visible: false,
      confirmLoading: false,
      addForm: {
        id: null,
        operatorId: null,
        operation: null,
        requestMethod: null,
        resourcePath: null,
        status: null,
        consumeTime: null,
        operationTime: null,
        clientIp: null,
        detailMsg: null,
        exceptionMsg: null,
        operator: null,
      },
      // 表单校验规则
      rules: {
        operatorId: [
          {
            required: true,
            message: "操作人ID不能为空！",
            trigger: "blur",
          },
        ],
        operation: [
          {
            required: true,
            message: "操作内容不能为空！",
            trigger: "blur",
          },
        ],
        requestMethod: [
          {
            required: true,
            message: "请求方式不能为空！",
            trigger: "blur",
          },
        ],
        resourcePath: [
          {
            required: true,
            message: "资源路径不能为空！",
            trigger: "blur",
          },
        ],
        status: [
          {
            required: true,
            message: "状态不能为空！",
            trigger: "blur",
          },
        ],
        operationTime: [
          {
            required: true,
            message: "操作时间不能为空！",
            trigger: "blur",
          },
        ],
      },
      // 日期选择快捷方式
      DateTime_Picker_Option,
    };
  },

  methods: {
    // 打开【系统日志】表单新增窗口
    openAddForm(isShow) {
      this.visible = isShow;
    },

    // 关闭【系统日志】表单新增窗口
    handleCancel() {
      this.visible = false;
      this.$refs["addForm"].resetFields();
    },

    // 提交
    handleSubmit() {
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            target: document.querySelector(".add-dialog"),
            lock: true,
            text: "正在保存，请耐心等待！",
          });

          let vueIns = this;
          // 请求参数
          let param = Object.assign({}, this.addForm);
          // 发起请求
          this.$api
            .addSysLog(param)
            .then((res) => {
              loading.close();
              let { status, message, data } = res.data; // 响应数据
              if (status == 200) {
                this.$emit("afterSubmit");
                this.handleCancel();
                // 提示信息
                this.$message({
                  type: "success",
                  message: `操作成功！已新增【系统日志】!${message}`,
                  duration: 1680,
                });
              } else if (status == 403) {
                this.$message({
                  message: `权限不足！${message}`,
                  type: "error",
                  center: true,
                  duration: 1680,
                });
                // this.$router.push("/login");
              } else {
                this.$alert(`操作失败！${message}`, "【系统日志】新增", {
                  type: "error",
                  confirmButtonText: "确定",
                  callback: (action) => {},
                });
              }
            })
            .catch(function(err) {
              loading.close();
              vueIns.visible = false;
              console.log(`【系统日志】新增接口发生错误：${err}`);
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
// 表单布局样式
@import "@/assets/css/form-layout.scss";
</style>
